import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    token: uni.getStorageSync('token') || '',
    userInfo: uni.getStorageSync('userInfo') || {},
    isLogin: false
  }),
  
  getters: {
    getUserInfo: (state) => state.userInfo,
    getToken: (state) => state.token,
    getLoginStatus: (state) => state.isLogin
  },
  
  actions: {
    // 设置用户信息
    setUserInfo(info) {
      this.userInfo = info
      uni.setStorageSync('userInfo', info)
    },
    
    // 设置token
    setToken(token) {
      this.token = token
      uni.setStorageSync('token', token)
    },
    
    // 设置登录状态
    setLoginStatus(status) {
      this.isLogin = status
    },
    
    // 登录
    login(userInfo) {
      this.setUserInfo(userInfo)
      this.setToken(userInfo.token)
      this.setLoginStatus(true)
    },
    
    // 登出
    logout() {
      this.setUserInfo({})
      this.setToken('')
      this.setLoginStatus(false)
      uni.removeStorageSync('token')
      uni.removeStorageSync('userInfo')
    }
  }
}) 